package cn.xx.order.service.impl;

import cn.xx.basic.constant.PayBillConstant;
import cn.xx.basic.domain.Point;
import cn.xx.basic.service.impl.BaseServiceImpl;
import cn.xx.basic.util.CodeGenerateUtils;
import cn.xx.basic.util.DistanceUtil;
import cn.xx.order.domain.OrderAddress;
import cn.xx.order.domain.ProductOrder;
import cn.xx.order.domain.ProductOrderDetail;
import cn.xx.order.mapper.OrderAddressMapper;
import cn.xx.order.mapper.ProductOrderDetailMapper;
import cn.xx.order.mapper.ProductOrderMapper;
import cn.xx.order.service.IProductOrderService;
import cn.xx.org.domain.Shop;
import cn.xx.org.mapper.ShopMapper;
import cn.xx.pay.domain.PayBill;
import cn.xx.pay.mapper.PayBillMapper;
import cn.xx.pay.service.IPayBillService;
import cn.xx.product.domain.Product;
import cn.xx.product.mapper.ProductMapper;
import cn.xx.quartz.config.MainJob;
import cn.xx.quartz.domain.QuartzInfo;
import cn.xx.quartz.service.IQuartzService;
import cn.xx.quartz.utils.QuartzUtils;
import cn.xx.user.domain.User;
import cn.xx.user.domain.UserAddress;
import cn.xx.user.mapper.UserAddressMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class ProductOrderServiceImpl extends BaseServiceImpl<ProductOrder> implements IProductOrderService {
    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private OrderAddressMapper orderAddressMapper;

    @Autowired
    private UserAddressMapper userAddressMapper;

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private ProductOrderMapper productOrderMapper;

    @Autowired
    private ProductOrderDetailMapper productOrderDetailMapper;

    @Autowired
    private PayBillMapper payBillMapper;

    @Autowired
    private IPayBillService payBillService;

    @Autowired
    private IQuartzService quartzService;


    @Transactional
    @Override
    public String createOrder(Map<String, String> map, User user) {
        // 获取前端传递的产品id
        Long productId =  Long.valueOf(map.get("productId"));
        // 查询产品对象
        Product product = productMapper.findById(productId);
        // 通过userid查询用户地址
        UserAddress userAddress = userAddressMapper.findById(user.getId());
        // 创建服务订单对象
        ProductOrder productOrder = createProductOrder(product,user,userAddress);
        // 保存服务订单对象
        productOrderMapper.save(productOrder);
        // 创建服务订单详细表对象
        ProductOrderDetail productOrderDetail = createProductOrderDetail(product);
        // 设置订单id
        productOrderDetail.setOrder_id(productOrder.getId());
        // 保存详细订单对象
        productOrderDetailMapper.save(productOrderDetail);
        // 创建订单地址表对象
        OrderAddress orderAddress = createOrderAddress(userAddress);
        // 设置订单id
        orderAddress.setOrder_id(productOrder.getId());
        // 设置订单编号
        orderAddress.setOrderSn(productOrder.getOrderSn());
        // 保存订单地址
        orderAddressMapper.save(orderAddress);
        // 添加定时任务
        QuartzInfo quartzInfo = new QuartzInfo();
        // 设置任务名称 product+订单编号
        quartzInfo.setJobName("product"+productOrder.getOrderSn());
        // 设置任务时间为最后支付时间
        quartzInfo.setTime(productOrder.getLastPayTime());
        // 设置任务类
        quartzInfo.setCls(MainJob.class);
        // 设置参数
        HashMap<String, Object> params = new HashMap<>();
        params.put("orderSn", productOrder.getOrderSn());
        quartzInfo.setParams(params);
        // 调用封装的方法添加任务
        quartzService.addJob(quartzInfo);

        // 创建支付单
        PayBill payBill = createPayBill(productOrder);
        // 保存支付单
        payBillMapper.save(payBill);

        // 调用提交方法
        String result = payBillService.submitPay(productOrder, payBill);
        // 返回提交的表单数据
        return result;
    }

    // 创建支付单
    private PayBill createPayBill(ProductOrder productOrder) {
        PayBill payBill = new PayBill();
        // 设置标题
        payBill.setDigest(productOrder.getDigest());
        // 设置金额
        payBill.setMoney(productOrder.getPrice());
        // 状态
        payBill.setState(productOrder.getState());
        // 最后支付时间
        payBill.setLastPayTime(productOrder.getLastPayTime());
        // 订单编号
        payBill.setOrderSn(productOrder.getOrderSn());
        // 支付方式
        payBill.setPayChannel(1L);
        // 创建时间
        payBill.setCreateTime(new Date());

        // 订单类型
        payBill.setBusinessType(PayBillConstant.PRODUCT_ORDER);
        // 订单id
        payBill.setBusinessKey(productOrder.getId());
        // 下单用户
        payBill.setUser_id(productOrder.getUser_id());
        // 店铺id
        payBill.setShop_id(productOrder.getShop_id());

        return payBill;
    }

    // 创建订单地址表对象
    private OrderAddress createOrderAddress(UserAddress userAddress) {
        // new 对象
        OrderAddress orderAddress = new OrderAddress();
        // 调用工具复制userAddress到orderAddress
        BeanUtils.copyProperties(userAddress, orderAddress);
        // 返回对象
        return orderAddress;
    }

    // 创建服务订单详细表对象
    private ProductOrderDetail createProductOrderDetail(Product product) {
        // new 对象
        ProductOrderDetail productOrderDetail = new ProductOrderDetail();
        // 调用方法直接复制product的数据到productOrderDetail
        BeanUtils.copyProperties(product, productOrderDetail);
        // 设置产品id
        productOrderDetail.setProduct_id(product.getId());
        return productOrderDetail;
    }

    // 创建服务订单对象
    private ProductOrder createProductOrder(Product product, User user,UserAddress userAddress) {
        // new productOrder对象
        ProductOrder productOrder = new ProductOrder();
        // 设置订单标题
        productOrder.setDigest("[服务订单]" + product.getName());
        // 设置最后的支付时间
        Date date = new Date(System.currentTimeMillis() + 1000 * 60 * 30);
        productOrder.setLastPayTime(date);
        productOrder.setLastConfirmTime(null);
        // 设置支付金额
        productOrder.setPrice(product.getSaleprice());
        // 设置用户id
        productOrder.setUser_id(user.getId());
        // 设置最近的店铺id
        Point point = DistanceUtil.getPoint(userAddress.getFullAddress());
        List<Shop> shops = shopMapper.findAll();
        Shop shop = DistanceUtil.getNearestShop(point, shops);
        productOrder.setShop_id(shop.getId());
        // 设置产品id
        productOrder.setProduct_id(product.getId());
        // 生成订单编号
        productOrder.setOrderSn(CodeGenerateUtils.generateOrderSn(user.getId()));
        // 默认状态0
        productOrder.setState(0);
        return productOrder;
    }
}
